|
|
|
|
|
|
|
Figure 19.2
A vector plus a vector equals a vector. |
|
|
|
|
|
|
|
|
Conversely, if C is a vector in the plane containing A and B, there is some i and j for which C = i * A + j * B. This gives you a way to generate points on a plane. Calculate vectors C for different values of i and j and add the resulting vectors to a point on the plane. The RayCheckerboard class described in Chapter 17, Ray Tracing, uses this technique. |
|
|
|
|
|
|
|
|
The length of a vector V = <vx, vy, vz> is written |V| and is given by Sqr(vx * vx + vy * vy + vz * vz). |
|
|
|
|
|
|
|
|
A vector V is normalized if |V| = 1. When you normalize a vector, you make its length 1 while keeping it pointed in its original direction. To normalize a vector, divide each of its components by the vector's length: <vx/|V|, vy/|V|, vz/|V|>. |
|
|
|
|
|
|
|
|
Normalized vectors are particularly useful with dot products and cross products. The lengths of the vectors play a role in these calculations, and if the vectors have length 1, the calculations are simpler. |
|
|
|
|
|
|
|
|
The dot product of two vectors A and B is written A B and has value |A| * |B| * Cos(q) where q is the angle between the two vectors. Mathematically, you can calculate the dot product by multiplying the corresponding components of two vectors and adding the results: |
|
|
|
 |
|
|
|
|
A B = ax * bx + ay * by + az * bz |
|
|
|
|
|
|
|
|
This gives you method for calculating the cosine of the angle between two vectors: |
|
|
|
 |
|
|
|
|
Cos(q) = (A B)/(|A| * |B|) |
|
|
|
|
|
|
|
|
If the vectors A and B are normalized, this simplifies to: |
|
|
|
 |
|
|
|
|
Cos (q) = A · B |
|
|
|
|
|